***************************************************************
****Replication file for Ryan E. Carlin & Timothy Hellwig "The World Economy, Political Control, and Presidential Success." forthcoming in The Journal of Politics***
***************************************************************

****************************
***modelling economic performance
****************************
use data_panel.dta, replace
sort countryid year

/*Table 1 & Table A1*/
reg dgdp l.dgdp geti lsce c.geti#c.lsce i.countryid if year>=startyear
eststo M1
estat vce 
reg inflognorm l.inflognorm geti lsce c.geti#c.lsce i.countryid if year>=startyear
eststo M2
estat vce 
reg unempnorm l.unempnorm geti lsce c.geti#c.lsce i.countryid if year>=startyear 
eststo M3
estat vce 
reg okun l.okun geti lsce c.geti#c.lsce i.countryid if year>=startyear
eststo M4 
estat vce 
reg hanke l.hanke geti lsce c.geti#c.lsce i.countryid if year>=startyear
eststo M5
estat vce 
estout M1 M2 M3 M4 M5, cells(b(star fmt(3)) se(par)) stats(N r2 bic, fmt(2)) legend label collabels(none) varlabels(_cons Constant) starlevels(+ 0.10 * 0.05 ** 0.01)

/*Table A2*/ 
reg dgdp l.dgdp geti lsce c.geti#c.lsce i.countryid if year>=startyear & ortneod~=.
eststo M1
reg dgdp l.dgdp geti ortneod c.geti#c.ortneod i.countryid if year>=startyear & ortneod~=.
eststo M2
reg inflognorm l.inflognorm geti lsce c.geti#c.lsce i.countryid if year>=startyear & ortneod~=.
eststo M3
reg inflognorm l.inflognorm geti ortneod c.geti#c.ortneod i.countryid if year>=startyear & ortneod~=.
eststo M4
reg unempnorm l.unempnorm geti lsce c.geti#c.lsce i.countryid if year>=startyear & ortneod~=.
eststo M5
reg unempnorm l.unempnorm geti ortneod c.geti#c.ortneod i.countryid if year>=startyear & ortneod~=.
eststo M6
reg okun l.okun geti lsce c.geti#c.lsce i.countryid if year>=startyear & ortneod~=.
eststo M7
reg okun l.okun geti ortneod c.geti#c.ortneod i.countryid if year>=startyear & ortneod~=.
eststo M8
reg hanke l.hanke geti lsce c.geti#c.lsce i.countryid if year>=startyear & ortneod~=.
eststo M9
reg hanke l.hanke geti ortneod c.geti#c.ortneod i.countryid if year>=startyear & ortneod~=.
eststo M10
estout M1 M2 M3 M4 M5, cells(b(star fmt(2)) se(par)) stats(N r2 aic bic rmse, fmt(3)) legend label collabels(none) varlabels(_cons Constant) starlevels(+ 0.10 * 0.05 ** 0.01)
estout M6 M7 M8 M9 M10, cells(b(star fmt(2)) se(par)) stats(N r2 aic bic rmse, fmt(3)) legend label collabels(none) varlabels(_cons Constant) starlevels(+ 0.10 * 0.05 ** 0.01)
clear

********************************
****modelling incumbent re-election
********************************
use data_elections.dta
gen neolib01 = dfa4
recode neolib01 min/0=0 .00001/max=1
gen stat01 = dfa2
recode stat01 min/0=0 .00001/max=1
gen ortneod01 = ortneod
recode ortneod01 min/0=0 .00001/max=1
gen loraindex01 = loraindex
recode loraindex01 min/.5=0 .5000001/max=1
label variable ortneod "Neoliberalism"
label variable reelecb "Reelection"
label variable geti12 "GET"
label variable lsce "LSCE"
sum geti12 if ortneod~=.
sum geti12
gen cid1 = 0 
recode cid1 *=1 if countryid==1
gen cid2 = 0 
recode cid2 *=1 if countryid==2
gen cid3 = 0 
recode cid3 *=1 if countryid==3
gen cid4 = 0 
recode cid4 *=1 if countryid==4
gen cid5 = 0 
recode cid5 *=1 if countryid==5
gen cid6 = 0 
recode cid6 *=1 if countryid==6
gen cid7 = 0 
recode cid7 *=1 if countryid==7
gen cid8 = 0 
recode cid8 *=1 if countryid==8
gen cid9 = 0 
recode cid9 *=1 if countryid==9
gen cid10 = 0 
recode cid10 *=1 if countryid==10
gen cid11 = 0 
recode cid11 *=1 if countryid==11
gen cid12 = 0 
recode cid12 *=1 if countryid==12
gen cid13 = 0 
recode cid13 *=1 if countryid==13
gen cid14 = 0 
recode cid14 *=1 if countryid==14
gen cid15 = 0 
recode cid15 *=1 if countryid==15
gen cid16 = 0 
recode cid16 *=1 if countryid==16
gen cid17 = 0 
recode cid17 *=1 if countryid==17
gen cid18 = 0 
recode cid18 *=1 if countryid==18
impute zlsce lsce year cid1 cid2 cid3 cid4 cid5 cid6 cid7 cid8 cid9 cid11 cid12 cid13 cid15 cid16 cid17 cid18, gen(im_zlsce)

/*Table A3*/
xtset countryid
/*fds refer to effect of moving from "bad" intl economy (GET mean - 1sd = -0.7797) to "good" intl economy (GET mean + 1sd = 1.1567)*/
/*model 1*/
logit reelecb c.geti12##c.lsce if free==1, cluster(country)
margins, dydx(geti12) at(geti12=(-.779 1.157) lsce=(1))
gen getilsce = geti12*lsce
estsimp logit reelecb geti12 lsce getilsce if free==1, cluster(country)
setx lsce 1
simqi, fd(pr) changex(geti12 -0.7797 1.1567 getilsce -0.7797 1.1567)
setx lsce 0 getilsce 0
simqi, fd(pr) changex(geti12 -0.7797 1.1567)
drop b*
eststo M1
/*model 2 - this gets same parms as CZ but sample is dropped from 106 to 96.  Must be typo in CZ*/
estsimp logit reelecb geti12 getilsce cid1 cid2 cid3 cid4 cid5 cid6 cid7 cid8 cid9 cid11 cid12 cid13 cid15 cid16 cid17 cid18  if free==1 & countryid~=10 & countryid~=14, nocon
setx mean
simqi, fd(pr) changex(geti12 -0.7797 1.1567 getilsce -0.7797 1.1567)
setx getilsce 0
simqi, fd(pr) changex(geti12 -0.7797 1.1567)
eststo M2
drop b*
/*model 3*/
xtlogit reelecb geti12 lsce getilsce if free==1, re
eststo M3
/*model 4*/
estsimp logit reelecb geti12 lsce getilsce incran if free==1, cluster(country)
setx lsce 1
simqi, fd(pr) changex(geti12 -0.7797 1.1567 getilsce -0.7797 1.1567)
setx lsce 0
simqi, fd(pr) changex(geti12 -0.7797 1.1567)
eststo M4
drop b*
/*model 5*/
estsimp logit reelecb geti12 lsce getilsce dgov if free==1, cluster(country)
setx lsce 1
simqi, fd(pr) changex(geti12 -0.7797 1.1567 getilsce -0.7797 1.1567)
setx lsce 0
simqi, fd(pr) changex(geti12 -0.7797 1.1567)
eststo M5
drop b*
estout M1 M2 M3 M4 M5, cells(b(star fmt(3)) se(par)) stats(N bic rmse, fmt(3)) legend label collabels(none) varlabels(_cons Constant) starlevels(+ 0.10 * 0.05 ** 0.01)

/*Table A4*/
xtset countryid
/*model 1*/
logit reelecb c.geti12##c.ipolortn if free==1, cluster(country)
eststo M1
/*model 2 - this gets same parms as CZ but sample is dropped from 106 to 96.  Must be typo in CZ*/
logit reelecb geti12 c.geti12#c.ipolortn cid1 cid2 cid3 cid4 cid5 cid6 cid7 cid8 cid9 cid10 cid11 cid12 cid13 cid14 cid15 cid16 cid17 cid18  if free==1, nocon
eststo M2
/*model 3*/
xtlogit reelecb c.geti12##c.ipolortn if free==1, re
eststo M3
/*model 4*/
logit reelecb geti12 ipolortn c.geti12#c.ipolortn incran if free==1, cluster(country)
eststo M4
/*model 5*/
logit reelecb geti12 ipolortn c.geti12#c.ipolortn dgov if free==1, cluster(country)
eststo M5
estout M1 M2 M3 M4 M5, cells(b(star fmt(3)) se(par)) stats(N bic rmse, fmt(3)) legend label collabels(none) varlabels(_cons Constant) starlevels(+ 0.10 * 0.05 ** 0.01)
impute zlsce lsce year cid1 cid2 cid3 cid4 cid5 cid6 cid7 cid8 cid9 cid10 cid11 cid12 cid13 cid14 cid15 cid16 cid17 cid18, gen(im_zlsce)

/*Table A5*/
xtset countryid
/*model 1*/
logit reelecb c.geti12##c.ortneod if free==1, cluster(country)
eststo M1
/*model 2 - this gets same parms as CZ but sample is dropped from 106 to 96.  Must be typo in CZ*/
logit reelecb geti12 c.geti12#c.ortneod cid1 cid2 cid3 cid4 cid5 cid6 cid7 cid8 cid9 cid10 cid11 cid12 cid13 cid14 cid15 cid16 cid17 cid18  if free==1, nocon
eststo M2
/*model 3*/
xtlogit reelecb c.geti12##c.ortneod if free==1, re
eststo M3
/*model 4*/
logit reelecb geti12 ortneod c.geti12#c.ortneod incran if free==1, cluster(country)
eststo M4
/*model 5*/
logit reelecb geti12 ortneod c.geti12#c.ortneod dgov if free==1, cluster(country)
eststo M5
estout M1 M2 M3 M4 M5, cells(b(star fmt(3)) se(par)) stats(N bic rmse, fmt(3)) legend label collabels(none) varlabels(_cons Constant) starlevels(+ 0.10 * 0.05 ** 0.01)

/*Table 2 & Figure 3*/
/*model 1 = m4 in CZ*/
logit reelecb geti12 lsce c.geti12#c.lsce incran if free==1, cluster(country)
eststo M1
quietly margins, at(incran=0 lsce=(1 0) geti12=(1.173 -.832)) atmean
marginsplot, scheme(s1mono) name(m1, replace) title("A. Table 2 Model 1") nodraw
/*redo with revised CONTINUOUS lsce measure*/
label variable im_zlcse "LSCE continuous"
logit reelecb geti12 im_zlsce c.geti12#c.im_zlsce incran if free==1, cluster(country)
eststo M2
quietly margins, at(incran=0 im_zlsce=(-2(1)2) geti12=(1.173 -.832)) atmean
marginsplot, scheme(s1mono) name(m2, replace) title("A. Table 2 Model 2") nodraw legend(off)
/*redo with non-interpolated regime measure*/
logit reelecb geti12 ortneod c.geti12#c.ortneod incran if free==1, cluster(country)
eststo M3
/*both interactions*/
logit reelecb geti12 lsce ortneod c.geti12#c.lsce c.geti12#c.ortneod incran if free==1, cluster(country)
eststo M4
quietly margins, at(incran=0 ortneod=(-3(1)3) geti12=(1.173 -.832)) atmean 
marginsplot, scheme(s1mono) name(m4a, replace) title("C. Table 2 Model 4, Neolib.") nodraw legend(off)
quietly margins, at(incran=0 lsce=(1 0) geti12=(1.173 -.832)) atmean 
marginsplot, scheme(s1mono) name(m4b, replace) title("B. Table 2 Model 4, LSCE") nodraw legend(off)
/*redo with non-interpolated regime measure, lsces only*/
logit reelecb geti12 ortneod c.geti12#c.ortneod incran if free==1 & ortneod~=. & lsce==1, cluster(country)
eststo M5
quietly margins, at(incran=0 ortneod=(-3(1)3) geti12=(1.173 -.832)) atmean 
marginsplot, scheme(s1mono) name(m5, replace) title("D. Table 2 Model 5") nodraw legend(off)
graph combine m2 m4b m4a m5, scheme(s1mono) ycommon c(2)
estout M1 M2 M3 M4 M5, cells(b(star fmt(3)) se(par)) stats(N bic, fmt(3)) legend label collabels(none) varlabels(_cons Constant) starlevels(+ 0.10 * 0.05 ** 0.01)
clear

***************************
****data pooled approval
***************************
use "data_approval.dta"
set more off
tsset countryid timevar, q
impute zlsce lsce year arg bol bra chi col cr dr ecu els mex par per ven, gen(im_zlsce)
/*****************Table 3 models, September 11, 2018*********/
/*set sample*/
quietly xtpcse appappdis i.ccode l.appappdis q_grow_pc honeymoon scan08 event q_vdem_corr geti_q lscecont ortneod if year>1989 & q_loraindex~=., c(psar1) pair
predict e_growth if e(sample)
/*Table 3*/
xtpcse appappdis i.ccode l.appappdis q_grow_pc honeymoon scan08 event q_vdem_corr geti_q if e_growth~=., c(psar1) pair
eststo M1
xtpcse appappdis i.ccode l.appappdis q_grow_pc honeymoon scan08 event q_vdem_corr geti_q lsce c.geti_q#c.lsce if e_growth~=., c(psar1) pair
estat vce
eststo M2
xtpcse appappdis i.ccode l.appappdis q_grow_pc honeymoon scan08 event q_vdem_corr geti_q lscecont c.geti_q#c.lscecont if e_growth~=., c(psar1) pair
eststo M3
xtpcse appappdis i.ccode l.appappdis q_grow_pc honeymoon scan08 event q_vdem_corr geti_q ortneod c.geti_q#c.ortneod if e_growth~=., c(psar1) pair
eststo M4
xtpcse appappdis i.ccode l.appappdis q_grow_pc honeymoon scan08 event q_vdem_corr geti_q lsce ortneod c.geti_q#c.lsce c.geti_q#c.ortneod if e_growth~=., c(psar1) pair
eststo M5
xtpcse appappdis i.ccode l.appappdis q_grow_pc honeymoon scan08 event q_vdem_corr geti_q ortneod c.geti_q#c.ortneod if e_growth~=. & lsce==1, c(psar1) pair
eststo M6
estout M1 M2 M3 M4 M5, cells(b(star fmt(3)) se(par)) stats(N r2, fmt(3)) legend label collabels(none) varlabels(_cons Constant) starlevels(+ 0.10 * 0.05 ** 0.01)

/*Figure 4*/
gen lag_appappdis = l.appappdis
gen getixlsce = geti_q*lsce
gen getixortn = geti_q*ortneod
qui xtpcse appappdis geti_q ortneod getixortn lsce getixlsce q_grow_pc honeymoon scan08 event q_vdem_corr lag_appappdis i.ccode if e_growth~=., c(psar1) pair

matrix b=e(b) 
matrix V=e(V)
scalar b1=b[1,1]
scalar b2=b[1,2]
scalar b3=b[1,3]
scalar varb1=V[1,1] 
scalar varb3=V[3,3]
scalar covb1b3=V[1,3] 

capture drop vectaxis me lower_ci upper_ci
gen vectaxis = .
gen me = .
gen lower_ci = .
gen upper_ci = .
local b = 1

foreach i of numlist -5(.25)5 {
	local n = 1
	qui gen conb = b1 + (b3*(`i'))
	local conse = sqrt(varb1+varb3*((`i')^2)+2*covb1b3*(`i'))
	qui gen x=1.96*`conse'
 	qui gen upper=conb+x
	qui gen lower=conb-x
	qui sum conb
		local me = `r(mean)'
		replace me = `me' in `b'
	qui sum lower 
		local lower_ci = `r(mean)'
		replace lower_ci = `lower_ci' in `b'
	qui sum upper
		local upper_ci = `r(mean)'
		replace upper_ci = `upper_ci' in `b'
	di
	di
	di "The marginal effect for GET is " `me'
	di "The confidence interval is " `lower_ci' 	"	and		"	`upper_ci'
	di "The modifying variable is = 	" `i'
	di 
	di
	capture drop conb* x* upper lower
	replace vectaxis = `i' in `b'
	local b = `b' + 1
	local n = `n' + 1
}

twoway (line me vectaxis, clpattern(solid) sort) (line lower_ci vectaxis, clpattern(dash) sort) (line upper_ci vectaxis, clpattern(dash) sort), yline(0) xtitle("Neoliberalism") ytitle("ME of GET index") legend(off) scheme(s1mono) 

/*Figure 5*/
/*download zip file at http://web.missouri.edu/~williamslaro/But%20Wait!--Replication.zip*/
/*place the following files in your working directory: dynsim.ado, dynsim_pcse, estsimp_pcse, simqi_pcse*/
/*citation: Williams, Laron K. and Guy D. Whitten. 2011. “Dynamic Simulations of Autoregressive Relationships.” Stata Journal 11(4):577-588*/
dynsim_pcse if e_growth~=., dv(appappdis) ldv(lag_appappdis) ldv_val(50) time(timevar) pv(countryid) scen1(q_grow_pc 2 honeymoon 0 scan08 0 q_vdem_corr .5 event 0 geti_q 1.1386 lsce 0 ortneod 3.03 getixortn 3.45 getixlsce 0) scen2(honeymoon 0 scan08 0 geti_q 1.1386 lsce 0 ortneod -2.16 getixortn -2.46 getixlsce 0) command(xtpcse) estoptions(c(psar1)) sims(10000) n(9) scheme(s1mono) title("B. Comparison Group") ytitle ("Approval") legend(label(1 "Neolib. high") label(2 "Neolib low")) xtitle("quarter + t") name(geti_comp, replace) nodraw
dynsim_pcse if e_growth~=., dv(appappdis) ldv(lag_appappdis) ldv_val(50) time(timevar) pv(countryid) scen1(q_grow_pc 2 honeymoon 0 scan08 0 q_vdem_corr .5 event 0 geti_q 1.1386 lsce 1 ortneod 3.03 getixortn 3.45 getixlsce 1.1386) scen2(honeymoon 0 scan08 0 geti_q 1.1386 lsce 1 ortneod -2.16 getixortn -2.46 getixlsce 1.21386) command(xtpcse) estoptions(c(psar1)) sims(10000) n(9) scheme(s1mono) title("A. LSCE Group") ytitle ("Approval") legend(label(1 "Neolib. high") label(2 "Neolib. low")) xtitle("quarter + t") name(geti_lsce, replace) nodraw
graph combine geti_lsce geti_comp, scheme(s1mono) cols(2) ycommon 
clear


clear
